﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using Data.Entity;
using Service;

namespace ECommerce.Administration.Controls
{
	public partial class ProductBasicInfo : AdminBaseUsercontrol
	{
		private IProductService _ProductService;
		private IShippingMethodService _ShippingMethodService;

		public int ProductID
		{
			get
			{
				if (ViewState["ProductID"] != null)
				{
					return (int)ViewState["ProductID"];
				}

				return CommonHelper.QueryStringInt("productid");
			}
			set
			{
				ViewState["ProductID"] = value;
			}
		}

		public int SaveTheProductBasicInfo()
		{
			if (_ProductService == null)
			{
				_ProductService = new ProductService();
			}

			var theProduct = _ProductService.GetProduct(ProductID);

			if (theProduct == null)
			{
				theProduct = new Product();
				SetTheProductValues(theProduct);
				theProduct.CreatedOn = DateTime.Now;

				theProduct.ProductID = _ProductService.CreateNewProduct(theProduct);
				this.ProductID = theProduct.ProductID;

				if (theProduct.ProductID > 0)
				{
					// save the product's shipping method
					SaveSupportedShippingMethods();

					return theProduct.ProductID;
				}
			}
			else
			{
				SetTheProductValues(theProduct);

				if (_ProductService.UpdateProduct(theProduct) > 0)
				{
					// save the product's shipping method
					SaveSupportedShippingMethods();

					return theProduct.ProductID;
				}
			}

			return -1;
		}

		private void SetTheProductValues(Data.Entity.Product theProduct)
		{
			if (SiteContext.Current.WorkingLanguage != null)
			{
				theProduct.AllowUserRatings = CheckAllowUserRating.Checked;
				theProduct.Deleted = false;
				theProduct.FullDescription = EditorFullDescription.Content;
				theProduct.LanguageID = SiteContext.Current.WorkingLanguage.LanguageId;
				decimal listPrice = decimal.Zero;
				decimal.TryParse(TextListPrice.Text.Trim(), out listPrice);
				theProduct.ListPrice = listPrice;
				theProduct.MetaKeywords = TextMetaKeywords.Text.Trim();
				theProduct.MetaTitle = TextMetaTitle.Text.Trim();
				theProduct.Name = TextProductName.Text.Trim();
				theProduct.Published = CheckPublished.Checked;
				int quantity = 0;
				int.TryParse(TextQuantity.Text.Trim(), out quantity);
				theProduct.Quantity = quantity;
				theProduct.Recommend = CheckRecommend.Checked;
				theProduct.ShortDescription = TextShortDescription.Text.Trim();
				theProduct.ShowOnHomePage = CheckShowOnHomePage.Checked;
				theProduct.Size = TextSize.Text.Trim();
				theProduct.SizeUnit = TextSizeUnit.Text.Trim();
				theProduct.UpdatedOn = DateTime.Now;
				decimal weight = decimal.Zero;
				decimal.TryParse(TextWeight.Text.Trim(), out weight);
				theProduct.Weight = weight;
				theProduct.WeightUnit = TextWeightUnit.Text.Trim();
				theProduct.ProductCode = TextProductCode.Text.Trim();
				decimal cashTicketLimit = decimal.Zero;
				decimal.TryParse(TextCashTicketLimit.Text.Trim(), out cashTicketLimit);
				theProduct.CashTicketLimit = cashTicketLimit;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (SiteContext.Current == null)
			{
				Server.Transfer("~Administration/AdminLogin.aspx");
			}

			if (!IsPostBack)
			{
				BindShippingMethod();

				InitValues();
			}
		}

		private void BindShippingMethod()
		{
			if (_ShippingMethodService == null)
			{
				_ShippingMethodService = new Service.ShippingMethodService();
			}

			var shippingMethods = _ShippingMethodService.GetAllShippingMethod();

			CheckListShippingMethod.DataSource = shippingMethods;
			CheckListShippingMethod.DataBind();

			if (ProductID > 0)
			{
				var supportedShippingMethods = _ShippingMethodService.GetShippingMethodByProduct(ProductID);

				if (supportedShippingMethods != null && supportedShippingMethods.Count > 0)
				{
					//var lstSupportedShippingMethods = supportedShippingMethods.ToList();

					//if (lstSupportedShippingMethods.Count > 0)
					//{
					var supportedShippingMethodIDs = new List<string>();

					foreach (var shippingMethodItem in supportedShippingMethods)
					{
						supportedShippingMethodIDs.Add(shippingMethodItem.ShippingMethodID.ToString());
					}

					foreach (ListItem checkBoxListItem in CheckListShippingMethod.Items)
					{
						checkBoxListItem.Selected = supportedShippingMethodIDs.Contains(checkBoxListItem.Value);
					}
					//}
				}
			}
		}

		private void SaveSupportedShippingMethods()
		{
			if (ProductID > 0)
			{
				if (_ShippingMethodService == null)
				{
					_ShippingMethodService = new Service.ShippingMethodService();
				}

				foreach (ListItem checkboxListItem in CheckListShippingMethod.Items)
				{
					var shippingMethodID = int.Parse(checkboxListItem.Value);

					if (checkboxListItem.Selected)
					{
						_ShippingMethodService.SaveProduct_ShippingMethod_Mapping(ProductID, shippingMethodID);
					}
					else
					{
						_ShippingMethodService.DeleteTheProduct_ShippingMethod_Mapping(ProductID, shippingMethodID);
					}
				}
			}
		}

		private void InitValues()
		{
			if (ProductID > 0)
			{
				if (_ProductService == null)
				{
					_ProductService = new Service.ProductService();
				}

				var prod = _ProductService.GetProduct(ProductID);

				TextListPrice.Text = prod.ListPrice.Value.ToString();
				TextMetaKeywords.Text = prod.MetaKeywords.ToString();
				TextMetaTitle.Text = prod.MetaTitle;
				TextProductName.Text = prod.Name;
				TextQuantity.Text = prod.Quantity.Value.ToString();
				TextShortDescription.Text = prod.ShortDescription;
				TextSize.Text = prod.Size;
				TextSizeUnit.Text = prod.SizeUnit;
				TextWeight.Text = prod.Weight.Value.ToString();
				TextWeightUnit.Text = prod.WeightUnit;
				EditorFullDescription.Content = prod.FullDescription;
				CheckPublished.Checked = prod.Published;
				CheckRecommend.Checked = (prod.Recommend.HasValue ? prod.Recommend.Value : false);
				CheckShowOnHomePage.Checked = prod.ShowOnHomePage;
				CheckAllowUserRating.Checked = prod.AllowUserRatings;
				TextProductCode.Text = prod.ProductCode != null ? prod.ProductCode : string.Empty;
			}
		}
	}
}